package com.example.scaffolding.util

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import junit.framework.Assert.assertTrue
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.lang.Exception
import java.util.concurrent.CountDownLatch

@Suppress("UNREACHABLE_CODE")
@RunWith(AndroidJUnit4::class)
class HttpDownloaderTest {
    private lateinit var context: Context
    private var countDownLatch: CountDownLatch? = null

    @Before
    fun setUp() {
        context = ApplicationProvider.getApplicationContext()
        countDownLatch = CountDownLatch(1)
    }

    @After
    fun tearDown() {
    }

    @Test
    fun download() {
        lateinit var result: String
        HttpDownloader.download(context, "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180611%2F322305deaa304f68967e9d44629e6bea.jpeg&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1625739294&t=975b0a30df059cddd17d832d069927e0", object:
            EventListener<String> {
            override fun onProgress(progress: Int) {
//                TODO("Not yet implemented")
//                result = Int.toString()
//                countDownLatch?.countDown()
            }

            override fun onSuccess(t: String) {
                result = "success"
                countDownLatch?.countDown()
            }

            override fun onError(e: Exception) {
                result = "error"
                countDownLatch?.countDown()
            }
        })
        countDownLatch?.await()
        when (result) {
            "error" -> assertTrue(false)
            else -> assertTrue(true)
        }
    }
}